package com.woniuxy.microaftersaleserver.consultmag.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microaftersaleserver.consultmag.mapper.ConsultMapper;
import com.woniuxy.microaftersaleserver.consultmag.service.IConsultService;
import com.woniuxy.microaftersaleserver.openFegin.MicroGoodsServerFeign;
import com.woniuxy.microaftersaleserver.openFegin.MicroUserServerFeign;
import com.woniuxy.microcommonserver.bean.aftersales_cyx.ConsultGoods;
import com.woniuxy.microcommonserver.bean.gy.GoodsInfo;
import com.woniuxy.microcommonserver.bean.user_hpd.UserInfo;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 商品咨询实现
 */
@Transactional(readOnly = true)
@Service
public class ConsultServiceImpl implements IConsultService {

    @Resource
    private ConsultMapper consultServiceMapper;
    @Resource
    private MicroUserServerFeign microBaseServerFeign;
    @Resource
    private MicroGoodsServerFeign microGoodsServerFeign;


    /**
     * 添加咨询记录
     * @param consultGoods
     */

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void addConsultRecord(ConsultGoods consultGoods) {
        consultServiceMapper.addConsultRecord(consultGoods);
    }


    /**
     *修改
     * @param consultGoods
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void updateConsultRecord(ConsultGoods consultGoods) {
        consultServiceMapper.updateConsultRecord(consultGoods);
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public void deleteConsultRecord(int id) {
        consultServiceMapper.deleteConsultRecord(id);
    }

    /**
     * 分页查询咨询记录
     * @param consultGoods
     * @return
     */
    @Override
    public List<Map> findByParams(ConsultGoods consultGoods) {

        List<Map> byParams = consultServiceMapper.findByParams(consultGoods);
        List<Map> list = new LinkedList<>();
        byParams.forEach(e->{
            Integer goods_id =(Integer) e.get("goods_id");
            Long user_id=(Long)e.get("user_id");
            String user = microBaseServerFeign.findOneById(user_id);
            String goods=microGoodsServerFeign.findGoodsById(goods_id);
            System.out.println(user);
            System.out.println(goods);
            UserInfo userInfo = JSONObject.parseObject(user).toJavaObject(UserInfo.class);
            GoodsInfo goodsInfo=JSONObject.parseObject(goods).toJavaObject(GoodsInfo.class);
            e.put("user_id",userInfo);
            e.put("goods_id",goodsInfo);
            list.add(e);
            
        });
        return list;
    }

    /**
     * 分页查询所有咨询记录
     * @param request
     * @param consultGoods
     * @return
     */
    @Override
    public PageInfo<ConsultGoods> findAll(PageRequest request, ConsultGoods consultGoods) {
        PageInfo<ConsultGoods> pageInfo= PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->consultServiceMapper.findAll(request,consultGoods));
        System.out.println("===================="+pageInfo);
        return pageInfo;
    }
}
